package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2024/5/4 9:26
 * @Version 1.0
 */
public class MarketAdminServiceImpl implements MarketAdminService {
    @Override
    public MarketAdmin login(String username, String password) {
        //紧接着应该在service中调用mapper，因为我们使用的是mybatis，所以不需要再去编写mapper实现类对象
        //mapper接口，mapper映射文件里面都是关于表的一系列的操作，其实可以通过模板去生成
        //逆向工程还可以帮助我们去生成实体类对象
        SqlSession session = MybatisUtils.getSession();
        MarketAdminMapper marketAdminMapper = session.getMapper(MarketAdminMapper.class);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        //criteria用来构造一系列的where条件
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();

        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        MarketAdmin marketAdmin = marketAdminMapper.selectOneByExample(marketAdminExample);
        session.commit();
        session.close();
        return marketAdmin;
    }

    @Override
    public List<MarketAdmin> list(Integer limit, Integer page, String username, String sort, String order) {
        SqlSession session = MybatisUtils.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.setOrderByClause(sort + " " + order);
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        PageHelper.startPage(page, limit);
        List<MarketAdmin> marketAdminList = mapper.selectByExampleSelective(marketAdminExample, MarketAdmin.Column.id, MarketAdmin.Column.username, MarketAdmin.Column.avatar, MarketAdmin.Column.roleIds);
        session.commit();
        session.close();
        return marketAdminList;
    }

    @Override
    public void create(MarketAdmin marketAdmin) {
        SqlSession session = MybatisUtils.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        mapper.insertSelective(marketAdmin);
        session.commit();
        session.close();
    }

    @Override
    public void update(MarketAdmin marketAdmin) {
        SqlSession session = MybatisUtils.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        mapper.updateByPrimaryKeySelective(marketAdmin);
        session.commit();
        session.close();
    }

    @Override
    public boolean updatePassword(String newPassword, String oldPassword) {
        SqlSession session = MybatisUtils.getSession();
        MarketAdminMapper marketAdminMapper = session.getMapper(MarketAdminMapper.class);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        //criteria用来构造一系列的where条件
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        //Todo 写死了
        criteria.andIdEqualTo(1);
        MarketAdmin marketAdmin = marketAdminMapper.selectOneByExample(marketAdminExample);
        if(StringUtils.equals(marketAdmin.getPassword(),oldPassword)){
            marketAdmin.setPassword(newPassword);
            marketAdminMapper.updateByExample(marketAdmin,marketAdminExample);
            session.commit();
            session.close();
            return true;
        }else return false;
    }
    @Override
    public void delete(MarketAdmin marketAdmin) {
        SqlSession session = MybatisUtils.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        mapper.deleteByPrimaryKey(marketAdmin.getId());
        session.commit();
        session.close();
    }
}
